package com.ruoyi.wx.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.wx.domain.RentalRecord;
import com.ruoyi.wx.service.IRentalRecordService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 租赁记录Controller
 * 
 * @author wt
 * @date 2020-12-13
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/wx/rentalRecord" )
public class RentalRecordController extends BaseController {

    private final IRentalRecordService iRentalRecordService;

    /**
     * 查询租赁记录列表
     */
    @PreAuthorize("@ss.hasPermi('wx:rentalRecord:list')")
    @GetMapping("/list")
    public TableDataInfo list(RentalRecord rentalRecord)
    {
        startPage();
        LambdaQueryWrapper<RentalRecord> lqw = Wrappers.lambdaQuery(rentalRecord);
        if (rentalRecord.getUserInfoId() != null){
            lqw.eq(RentalRecord::getUserInfoId ,rentalRecord.getUserInfoId());
        }
        if (rentalRecord.getAccountId() != null){
            lqw.eq(RentalRecord::getAccountId ,rentalRecord.getAccountId());
        }
        if (rentalRecord.getRegionId() != null){
            lqw.eq(RentalRecord::getRegionId ,rentalRecord.getRegionId());
        }
        if (rentalRecord.getModelId() != null){
            lqw.eq(RentalRecord::getModelId ,rentalRecord.getModelId());
        }
        if (rentalRecord.getSegmentId() != null){
            lqw.eq(RentalRecord::getSegmentId ,rentalRecord.getSegmentId());
        }
        if (StringUtils.isNotBlank(rentalRecord.getWxId())){
            lqw.eq(RentalRecord::getWxId ,rentalRecord.getWxId());
        }
        if (StringUtils.isNotBlank(rentalRecord.getWxh())){
            lqw.eq(RentalRecord::getWxh ,rentalRecord.getWxh());
        }
        if (StringUtils.isNotBlank(rentalRecord.getWxRemark())){
            lqw.eq(RentalRecord::getWxRemark ,rentalRecord.getWxRemark());
        }
        if (StringUtils.isNotBlank(rentalRecord.getWxName())){
            lqw.like(RentalRecord::getWxName ,rentalRecord.getWxName());
        }
        if (StringUtils.isNotBlank(rentalRecord.getToWxId())){
            lqw.eq(RentalRecord::getToWxId ,rentalRecord.getToWxId());
        }
        if (rentalRecord.getState() != null){
            lqw.eq(RentalRecord::getState ,rentalRecord.getState());
        }
        if (rentalRecord.getRentalStartTime() != null){
            lqw.eq(RentalRecord::getRentalStartTime ,rentalRecord.getRentalStartTime());
        }
        if (rentalRecord.getRentalEndTime() != null){
            lqw.eq(RentalRecord::getRentalEndTime ,rentalRecord.getRentalEndTime());
        }
        if (rentalRecord.getTotalAmount() != null){
            lqw.eq(RentalRecord::getTotalAmount ,rentalRecord.getTotalAmount());
        }
        if (rentalRecord.getUserActiveRecycling() != null){
            lqw.eq(RentalRecord::getUserActiveRecycling ,rentalRecord.getUserActiveRecycling());
        }
        if (rentalRecord.getSystemCompulsoryRecyclingTime() != null){
            lqw.eq(RentalRecord::getSystemCompulsoryRecyclingTime ,rentalRecord.getSystemCompulsoryRecyclingTime());
        }
        if (rentalRecord.getPrice() != null){
            lqw.eq(RentalRecord::getPrice ,rentalRecord.getPrice());
        }
        if (rentalRecord.getUsedAmount() != null){
            lqw.eq(RentalRecord::getUsedAmount ,rentalRecord.getUsedAmount());
        }
        if (rentalRecord.getBalance() != null){
            lqw.eq(RentalRecord::getBalance ,rentalRecord.getBalance());
        }
        if (rentalRecord.getAccountInfoId() != null){
            lqw.eq(RentalRecord::getAccountInfoId ,rentalRecord.getAccountInfoId());
        }
        List<RentalRecord> list = iRentalRecordService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出租赁记录列表
     */
    @PreAuthorize("@ss.hasPermi('wx:rentalRecord:export')" )
    @Log(title = "租赁记录" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(RentalRecord rentalRecord) {
        LambdaQueryWrapper<RentalRecord> lqw = new LambdaQueryWrapper<RentalRecord>(rentalRecord);
        List<RentalRecord> list = iRentalRecordService.list(lqw);
        ExcelUtil<RentalRecord> util = new ExcelUtil<RentalRecord>(RentalRecord. class);
        return util.exportExcel(list, "rentalRecord" );
    }

    /**
     * 获取租赁记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('wx:rentalRecord:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iRentalRecordService.getById(id));
    }

    /**
     * 新增租赁记录
     */
    @PreAuthorize("@ss.hasPermi('wx:rentalRecord:add')" )
    @Log(title = "租赁记录" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RentalRecord rentalRecord) {
        return toAjax(iRentalRecordService.save(rentalRecord) ? 1 : 0);
    }

    /**
     * 修改租赁记录
     */
    @PreAuthorize("@ss.hasPermi('wx:rentalRecord:edit')" )
    @Log(title = "租赁记录" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RentalRecord rentalRecord) {
        return toAjax(iRentalRecordService.updateById(rentalRecord) ? 1 : 0);
    }

    /**
     * 删除租赁记录
     */
    @PreAuthorize("@ss.hasPermi('wx:rentalRecord:remove')" )
    @Log(title = "租赁记录" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iRentalRecordService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
